package com.googlecode.flealancemarket.tapestry.pages;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.Category;
import com.googlecode.flealancemarket.metamodel.CategoryGroup;
import com.googlecode.flealancemarket.metamodel.Job;
import com.googlecode.flealancemarket.service.commands.BrowseCategories;
import com.googlecode.flealancemarket.service.commands.SaveJob;
import java.util.List;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.ioc.annotations.Inject;
import com.googlecode.flealancemarket.tapestry.util.Session;

public class JobCreation {

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.BrowseCategories")
    private BrowseCategories categoryBrowser;

    @ApplicationState
    private Session session;

    private List<CategoryGroup> categoryGroups;
    private CategoryGroup categoryGroup;
    private Category category;
    private long categoryId;
    private int lowerBudget = 1;
    private int upperBudget;
    private int estimatedTime = 1;
    private java.util.Date availableUntil;
    private String jobDescription;
    
    public List<CategoryGroup> getCategoryGroups() {
        return (List<CategoryGroup>)categoryBrowser.execute();
    }

    public void setCategoryGroups(List<CategoryGroup> categoryGroups) {
        this.categoryGroups = categoryGroups;
    }

    public CategoryGroup getCategoryGroup() {
        return categoryGroup;
    }

    public void setCategoryGroup(CategoryGroup categoryGroup) {
        this.categoryGroup = categoryGroup;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(long categoryId) {
        this.categoryId = categoryId;
    }

    public int getLowerBudget() {
        return lowerBudget;
    }

    public void setLowerBudget(int lowerBudget) {
        this.lowerBudget = lowerBudget;
    }

    public int getUpperBudget() {
        return upperBudget;
    }

    public void setUpperBudget(int upperBudget) {
        this.upperBudget = upperBudget;
    }

    public int getEstimatedTime() {
        return estimatedTime;
    }

    public void setEstimatedTime(int estimatedTime) {
        this.estimatedTime = estimatedTime;
    }

    public java.util.Date getAvailableUntil() {
        return availableUntil;
    }

    public void setAvailableUntil(java.util.Date availableUntil) {
        this.availableUntil = availableUntil;
    }

    public String getJobDescription() {
        return jobDescription;
    }

    public void setJobDescription(String jobDescription) {
        this.jobDescription = jobDescription;
    }

    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.Job")
    private Job job;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.SaveJob")
    private SaveJob saveJob;

    Object onActivate(){
        if(!session.getUserIsEmployer())
            return Login.class;
        return null;
    }
    Object onSubmitFromJobCreationForm() {
        java.sql.Date sqlDate = new java.sql.Date(availableUntil.getTime());

        job.setDescription(jobDescription);
        job.setEndingTime(sqlDate);
        job.setEstimatedTime(estimatedTime);
        job.setLowerBudget(lowerBudget);
        job.setUpperBudget(upperBudget);

        saveJob.setJob(job);
        saveJob.setAuthorId(session.getUserId());
        saveJob.setCategoryId(categoryId);

        Job j = saveJob.execute();
        if( j==null )
            session.setMessage("Job hasn't been saved!");
        else
            session.setMessage("Job has been saved successfully!");
        return Categories.class;
    }

}
